package com.rongshu.view.submitArticle.presenter;

import android.content.Context;
import android.content.Intent;

import com.rongshu.api.RetorfitService;
import com.rongshu.utils.Common;
import com.rongshu.utils.Constant;
import com.rongshu.utils.UserInfoUtils;
import com.rongshu.view.base.BaseActivity;
import com.rongshu.view.base.mvp.BasePresenter;
import com.rongshu.view.base.mvp.INetBean;
import com.rongshu.view.base.mvp.INetView;
import com.rongshu.view.login.entity.PasswordLogin;
import com.rongshu.view.penpal.bean.ArticleColumnsResponse;
import com.rongshu.view.penpal.bean.ArticleLabelsResponse;
import com.rongshu.view.submitArticle.bean.LabelBean;
import com.rongshu.view.submitArticle.bean.SavaDraftBody;
import com.rongshu.view.submitArticle.bean.SubmitArticleBody;
import com.rongshu.view.web.LocalWebActivity;
import com.utils.LogUtil;
import com.widget.toast.ToastUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import rx.Subscriber;
import rx.functions.Action0;

/**
 * created by zhengweis on 2018/11/19.
 * description：
 */
public class ArticleSettingPresenter extends BasePresenter<INetView> {
    private int userId;
    public static int REQUEST_COLUMN_MSG = 101;//栏目集合
    public static int REQUEST_LABEL_MSG = 103;//标签集合
    public static int REQUEST_SUBMIT_ARTICLE = 102;//确认投稿
    private BaseActivity activity;

    public ArticleSettingPresenter(Context context) {
        super(context);
        PasswordLogin.ResponseBean userInfo = UserInfoUtils.getUserInfo();
        if (userInfo != null) {
            userId = userInfo.getUser_id();
        }
        activity = (BaseActivity) mContext;
    }

    public void getColumnList() {
        Map<String, Object> map = new HashMap<>();
        map.put("user_id", UserInfoUtils.getUserId());
        RetorfitService.getSubmitArticleColumns(UserInfoUtils.getUserId(),
                Common.encryptionSign(map)).doOnSubscribe(new Action0() {
            @Override
            public void call() {

            }
        }).subscribe(new Subscriber<ArticleColumnsResponse>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onNext(ArticleColumnsResponse object) {
                getView().success(object, REQUEST_COLUMN_MSG);
            }
        });
    }

    public void getLabelList(int theme_id) {
        ((BaseActivity)mContext).showLoading(true);
        Map<String, Object> map = new HashMap<>();
        map.put("user_id", UserInfoUtils.getUserId());
        map.put("theme_id", theme_id);
        RetorfitService.getSubmitArticleLabels(UserInfoUtils.getUserId(), theme_id,
                Common.encryptionSign(map)).doOnSubscribe(new Action0() {
            @Override
            public void call() {

            }
        }).subscribe(new Subscriber<ArticleLabelsResponse>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {
                ((BaseActivity)mContext).showLoading(false);
            }

            @Override
            public void onNext(ArticleLabelsResponse object) {
                ((BaseActivity)mContext).showLoading(false);
                getView().success(object, REQUEST_LABEL_MSG);
            }
        });
    }

    /**
     * @param body          保存草稿的body
     * @param lianzai_id    文集ID
     * @param wenzhang_list 所选文章分类
     * @param selectLabels  所选标签
     * @param topImg        头图
     */
    public void submitArticle(SavaDraftBody body, int draftId, int lianzai_id, int wenzhang_list
            , List<LabelBean> selectLabels, String topImg, int contentLength) {
        activity.showLoading(true);
        List<Integer> labelList = new ArrayList<>();

        Map<String, Object> map = new HashMap<>();
        map.put("user_id", userId);
        map.put("cg_id", draftId);
        map.put("lianzai_id", lianzai_id);
        map.put("isserial", false);
        map.put("number", contentLength);
        SubmitArticleBody submitArticleBody = new SubmitArticleBody();
        submitArticleBody.setTopImg(topImg);
        submitArticleBody.setTitle(body.getTitle());
        String articleContent = "<div>" + body.getContent() + "</div>";
        submitArticleBody.setContent(articleContent);
        submitArticleBody.setWenzhang_list(wenzhang_list);
        if (selectLabels != null && selectLabels.size() != 0) {
            for (LabelBean bean : selectLabels) {
                labelList.add(bean.getId());
            }
        }
        submitArticleBody.setGuanjianci(labelList);
        LogUtil.d("投稿成功--" + submitArticleBody.getContent());
        RetorfitService.submitArticle(userId, draftId, lianzai_id, false, contentLength,
                Common.encryptionSign(map), submitArticleBody).doOnSubscribe(new Action0() {
            @Override
            public void call() {

            }
        }).subscribe(new Subscriber<INetBean>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {
                activity.showLoading(false);
            }

            @Override
            public void onNext(INetBean object) {
                activity.showLoading(false);
                if (object.getErrCode() == 0) {
                    getView().success(null, REQUEST_SUBMIT_ARTICLE);
                } else {
                    ToastUtils.show(object.getErrMsg());
                }
            }
        });
    }

    public void toWebAct() {
        Intent intent = new Intent(mContext, LocalWebActivity.class);
        intent.putExtra(Constant.INTENT_TITLE, "投稿协议");
        intent.putExtra(Constant.INTENT_H5, Common.SUBMIT_ARTICLE_NOTICE);
        mContext.startActivity(intent);
    }
}
